﻿using Adapters.Adapters.Adapters;
using Adapters.Adapters.Interfaces;
using Adapters.Data;
using Adapters.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adapters.Controllers
{
    public class HomeController : Controller
    {
        private IToDoAdapter adapter;

        public HomeController()
        {
            adapter = new ToDoDataAdapter(); //this is the adapter on standard run
        }

        public HomeController(IToDoAdapter _adapter)
        {
            adapter = _adapter;
        }

        public ActionResult Index()
        {
            List<ToDo> model = adapter.GetAllTasks();
            return View(model);
        }

        public ActionResult AddToDo()
        {
            return View(new ToDo());
        }

        [HttpPost]
        public ActionResult AddToDo(ToDo model)
        {
            model.DateCompleted = null;
            model.DateCreated = DateTime.Now;

            int result = adapter.AddTask(model);

            if (result != 1)
            {
                ViewBag.Message = "There was an error adding your Task into the Database.";
                return View(model);
            }

            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}